function extract_params( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
	return "";
	else
	return results[1];
}

function updateEntryinTimestampsUI(obj,categoryName){
	var parentRow = $(obj).closest('tr');
	parentRow.children('.timestamps-category').eq(0).children("span").eq(0).text(categoryName);
	parentRow.children('.timestamps-category').eq(0).children("span").eq(0).removeClass("uncat");
	parentRow.children('.timestamps-category').eq(0).children("span").eq(0).addClass("category");
}



function ajax_categorize_summaries(category_id,category_name){
	var active = getIds("activity[]");
	var entries = getIds("entry[]",category_name);
	var aways = getIds("away_summary[]");
	var category_type = $('#category_type').attr('value');
	if (extract_params( 'range') != "")
	$.post('/summaries/ajax_categorize_2',{ type:category_type, category:category_id, away:aways, entries: entries, activities: active , start:extract_params( 'start'), range:extract_params( 'range' )});
	else if (extract_params( 'end') != "")
	$.post('/summaries/ajax_categorize_2',{ type:category_type, category:category_id,away:aways, entries: entries, activities: active , start:extract_params( 'start'),  end:extract_params( 'end' )});
	else
	$.post('/summaries/ajax_categorize_2',{ type:category_type, category:category_id,away:aways, entries: entries, activities: active , start:extract_params( 'start')});
}

function getIds(checkList,category_name){
	var idList = "";
	var loopCounter = 0;
	jQuery("input[name=" + checkList + "]:checked").each
	(
		function()
		{
				if (checkList == "entry[]")
				{
					updateEntryinTimestampsUI(this,category_name);
				}	
				if (checkList != "entry[]"){
					decrementParentRows(this);

					removeRow(this);
				}
				if (loopCounter == 0)

				idList =jQuery(this).val();
				else
				idList += ","+jQuery(this).val();
				loopCounter += 1;
				//}
			}

		);
		return idList;
	}

	function isManualEntry(checkbox){
		currentRow = $(checkbox).closest('tr');//.css('background-color', 'red');
		if ( currentRow.attr('class') == "summary-card child chrometa_manual_entry_exe selected")
		return true;
		else 
		return false;
	}






	function removeRow(checkboxInput){
		$(checkboxInput).closest('tr').remove();
	}

	function valueToDecrement(checkboxInput){
		var decrement = $(checkboxInput).closest('td').next().next().next().attr("seconds");
		return decrement;
	}

	//css('background-color', 'red')


	function decrementParentRow(parentRow,value){
		var parentRowOldVal = parentRow.children('.duration').attr("seconds");
		parentRow.children('.duration').attr("seconds",parentRowOldVal-value);
		parentRow.children('.duration').text(displayDuration(parentRowOldVal-value));
	}

	function decrementParentRows(checkboxInput){
		var value = valueToDecrement(checkboxInput);
		var grandParentRow = $(checkboxInput).closest('tr').prevAll('tr.summary-card-title:first');
		var parentRow = $(checkboxInput).closest('tr').prevAll('tr.summary-card.parent:first');
		decrementParentRow(parentRow,value);
		decrementParentRow(grandParentRow,value);


	}
